package com.libui.navigatonbar

import com.libbase.MapCodable
import com.libbase.cast

data class FontData(val fontName: String?, val fontSize: Float, val fontWeight: Float?) :
    MapCodable {
    companion object {
        private val fontWeights: Map<String, Float> =
            mapOf(
                "normal" to 400F,
                "regular" to 400F,
                "medium" to 500F,
                "bold" to 600F,
            )

        fun create(value: Any): FontData? {
            if (value is Number) {
                return FontData(null, value.toFloat(), null)
            } else {
                val map = cast<Map<String, Any>>(value) ?: return null
                val fontSize: Float =
                    cast<Number>(map["fontSize"])?.toFloat() ?: return null

                val fontWeight: Float? = (map["fontWeight"] as? String)?.let { fontWeights[it] }
                    ?: cast<Number>(map["fontWeight"])?.toFloat()

                return FontData(map["fontName"] as? String, fontSize, fontWeight)
            }
        }
    }

    override fun toMap(): Map<String, Any> {
        val map = mutableMapOf<String, Any>("fontSize" to fontSize)

        if (fontName != null) {
            map["fontName"] = fontName
        }

        if (fontWeight != null) {
            map["fontWeight"] = fontWeight
        }

        return map
    }
}